package top.linyuxb.lycloud.common.common;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import top.linyuxb.lycloud.common.properties.LyConfigProperties;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/6/13 16:02
 * @Desc: jdbc工具
 */
@Slf4j
public class LyConfigJdbcService {
    /**
     * 创建好数据库连接池对象
     * 不用创建连接对象因为数据库连接池对象就可以创建连接对象
     */
    private static DataSource ds;
    private static Boolean isInit = false;
    private static final String DEFAULT_DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";

    /**
     * 初始化dataSource
     *
     * @param driverClassName
     * @param url
     * @param username
     * @param password
     */
    public static void initDataSource(String driverClassName, String url, String username, String password) {
        if (isInit) {
            return;
        }
        Properties pro = new Properties();
        pro.put("driverClassName", StrUtil.blankToDefault(driverClassName, DEFAULT_DRIVER_CLASS));
        pro.put("url", url);
        pro.put("username", username);
        pro.put("password", password);
        initDataSource(pro);
        isInit = true;
    }

    /**
     * 初始化dataSource
     *
     * @param properties
     */
    public static void initDataSource(LyConfigProperties properties) {
        if (isInit) {
            return;
        }
        Properties pro = new Properties();
        pro.put("driverClassName", StrUtil.blankToDefault(properties.getDriver(), DEFAULT_DRIVER_CLASS));
        pro.put("url", properties.getUrl());
        pro.put("username", properties.getUsername());
        pro.put("password", properties.getPassword());
        initDataSource(pro);
        isInit = true;
    }

    /**
     * 初始化dataSource
     */
    public static void initDataSource(Properties pro) {
        try {
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            log.error("JdbcUtil initDataSource error", e);
        }
    }

    /**
     * 获取数据库连接池对象
     *
     * @return
     */
    public static DataSource getDataSource() {
        return ds;
    }

    /**
     * 获取连接对象
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    /**
     * 获取JdbcTemplate
     *
     * @return
     * @throws SQLException
     */
    public static JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(getDataSource());
    }

    /**
     * 归还连接对象
     *
     * @param res
     * @param stat
     * @param conn
     */
    public static void close(ResultSet res, Statement stat, Connection conn) {
        if (res != null) {
            try {
                res.close();
            } catch (Exception e) {
                log.error("JdbcUtil close res error", e);
            }
        }

        if (stat != null) {
            try {
                stat.close();
            } catch (Exception e) {
                log.error("JdbcUtil close stat error", e);
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                log.error("JdbcUtil close conn error", e);
            }
        }
    }
}
